<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%> 
		<script type="text/javascript">
	//状态
	var state_store = new Ext.data.JsonStore( {
		fields : ["id", "text"],
		data:[{id:"0",text:"停用"},{id:"1",text:"启用"},
			  {id:"2",text:"草稿"},{id:"3",text:"退回"}]
	});
	//卡类型下拉列表store
	var type_store= new Ext.data.JsonStore( {
		fields : ["id", "text"],
		data:[{id:"1",text:"纸质"},{id:"0",text:"电子"},{id:"2",text:"短信"}]
	}); 
	//p1显示列
	var p1_store = new Ext.data.JsonStore( {
		root : "data",
		idProperty:"",
		totalProperty: "total",
		fields : ["id","businpnt_no","foldout_name","foldout_prod_name","foldout_prod_weight",
				"foldout_pri","businpnt_name","period","period_begen","period_end","prod_no","prod_name"],
		url : "foldOut!listByCard.action"
	});
	//p2显示 列
	var p2_store = new Ext.data.JsonStore( {
		root : "data",
		fields : ["foldout_index","foldout_prod_name","price","foldout_prod_weight","foldout_pri"],
		url : "foldOut!listFoldoutDetails.action"
	});
	//生成插页id
	/*var feId_store = new Ext.data.JsonStore( {
		root : "data",
		fields : ["id"],
		url : "foldOut!feId.action"
	});*/
	//外包商下拉列表
	var bus_store=new Ext.data.JsonStore( {
		root : "data",
		fields : ["id","text"],
		url :"foldOut!listEpiboly.action"
	});
	//条件查询外包商下拉列表
	var bus_store1=new Ext.data.JsonStore( {
		root : "data",
		fields : ["id","text"],
		url :"foldOut!listEpiboly.action",
		listeners : {
			'load': function() {
				this.insert(0,new sys_store.recordName({id:'',text: '全部'}));
			}
		}
	});
	bus_store.load();
	bus_store1.load({callback:function(){
		Ext.getCmp("types").setValue("");
		Ext.getCmp("times").setValue("");
	}});
	
	//提交
	var opr_type;//传给ajax url属性的action字符串
	var page_param={};//传值参数对象
	
	function pageRequest(){
		var url="";
		if("add"==opr_type){
			url = "organization!add.action";
			page_param = {};
		}else if("edit"==opr_type){
			url="organization!update.action";
			page_param = {};
		}else if("del"==opr_type){
			url="organization!delete.action";
			page_param = {};
			var record =Ext.getCmp("org_grid").getSelectionModel().getSelected();
			page_param={
				id:record.data.id,
				supperId:record.data.supperId
			};
		}	
		//ajax提交
    	Ext.Ajax.request( {
    		url:url,
    		params:page_param,
    		success: function(response, opts){
				var d=eval("("+response.responseText+")");
				if(d.success){
					//Ext.Msg.alert("成功","操作成功");
					Ext.Msg.show({
		   				title:"提示",
		   				msg:"操作成功!",
		   				buttons:Ext.Msg.OK,
		   				icon: Ext.MessageBox.INFO
					});
					store.load({params:{parentId:page_param.supperId}});
					Ext.getCmp("foldout_tree").getNodeById(page_param.supperId).reload();
					Ext.getCmp("foldout_panel").layout.setActiveItem(0);
				} else {
					//Ext.Msg.alert("失败","操作失败");
					Ext.Msg.show({
		   				title:"提示",
		   				msg:"操作失败!",
		   				buttons:Ext.Msg.OK,
		   				icon: Ext.MessageBox.ERROR
						});
				}
			},
			failure: function(response, opts){
				
			}
    	});
	}
	
	
	var p1 = {
		id : "p1Grid",
		xtype : "grid",
		autoheight : true, 
		loadMask : true,//在加载数据时遮罩住元素,替换元素Updater的加载提示器，并且在初始化加载之后销毁
		autoExpandColumn:"businpnt_name",//自动填充
		autoExpandMin : 100,//自动填充最小宽度
		store :p1_store,
		headersDisabled: true,
		columns : [ new Ext.grid.RowNumberer( {header : "序号",width:35}), 
				{dataIndex : "id",header : "插页编号",hidden:true, menuDisabled: true}, 
				{dataIndex : "prod_name",header : "卡产品名称", menuDisabled: true}, 
	            {dataIndex : "businpnt_name",header : "外包商名称", menuDisabled: true,id:"businpnt_name"},                  
	            {dataIndex : "period_begen",header : "开始月", menuDisabled: true},
	            {dataIndex : "period_end",header : "截止月", menuDisabled: true},
	            {dataIndex : "period",header : "账单日", menuDisabled: true}
		],
		//查询条件
		tbar:[
			"&nbsp&nbsp外包商:",
			{id:"types", xtype:"billCombox", store:bus_store1, width:120,
				autoSelect:true,
				listeners:{
		         "select": function(comb,rcord,index){
		         	var node = Ext.getCmp("foldout_tree").getSelectionModel().getSelectedNode();
		         	if(node&&node.isLeaf()){
		         			if(""==this.getValue()){
		         				p1_store.baseParams["fp.businpnt_no"] = null;
		         			}else{
		         				p1_store.baseParams["fp.businpnt_no"] = this.getValue();
		         			}
		    	         	p1_store.load({params :{start:0,limit:limit}}); //根据选中节点动态刷新列表
			        	}
			        	else
			        	{
			        		alertMsg("没有选中任何卡产品，无法进行外包商条件查询操作!");
			        	}
		         	}
		    	}
			},"&nbsp&nbsp账单日:",
			{id:"times", xtype:"billCombox", store:sys_store.seachStmtdate, width: 100,
				listeners:{
			    	"select": function(comb,rcord,index){
			         	var node = Ext.getCmp("foldout_tree").getSelectionModel().getSelectedNode();
			         	if(node&&node.isLeaf()){
			         		if(""==this.getValue()){
			         			p1_store.baseParams["fp.period"] = null;
			         		}else{
			         			p1_store.baseParams["fp.period"] = this.getRawValue();
			         		}
		    	         	p1_store.load({params :{start:0,limit:limit}}); //根据选中节点动态刷新列表
						} else {
							alertMsg("没有选中任何卡产品，无法进行账单日条件查询操作!");
			        	}
			        }
			    }
			}
		],
		buttonAlign : "center",
		bbar : new Ext.ux.PagingToolbar( {
			id:"review",
			pageSize : limit,
			store : p1_store
		}),
		buttons : [
				{
					text : "新增",
					handler : function() {
						opr_type = "foldOut!addfocedate.action";
						var node=Ext.getCmp("foldout_tree").getSelectionModel().getSelectedNode();//当前选中的树节点
						if(node&&node.isLeaf()){
							Ext.getCmp("areaform").resetParam(node.id,node.text);
							p2_store.removeAll();
							Ext.getCmp("foldout_panel").layout.setActiveItem(1);
						}else{
							alertMsg("没有选中任何行信息，无法进行新增操作!");
						}
					}
				},{
					text : "修改",
					handler : function() {
						opr_type = "foldOut!updfocedate.action";
						var record = Ext.getCmp("p1Grid").getSelectionModel().getSelected();
						if (record) {
							Ext.getCmp("foldout_panel").layout.setActiveItem(1);
							Ext.getCmp("areaform").setParam(record.data);
						}else{
							alertMsg("没有选中任何行信息，无法进行修改操作!");
						}
					}
				},
				{
					text : "删除",
					handler : function() {
						opr_type = "foldOut!deltsfps.action";
						var p1 = this.ownerCt.ownerCt;
						var record = p1.getSelectionModel().getSelected();
						if (record) {
							Ext.Msg.confirm("警告","确定要删除吗?", function(btn){
							    if (btn == "yes"){
							    	Ext.Ajax.request( {
							    		url : opr_type,
							    		params : {"fp.id":record.data.id},
							    		success: function(response, opts){
											var d=eval("("+response.responseText+")");
											if(d.code == "1"){
												Ext.getCmp("review").doRefresh();
												//Ext.Msg.alert("成功",d.data);
												Ext.Msg.show({
		   											title:"提示",
		   											msg:"操作成功!",
		   											buttons:Ext.Msg.OK,
		   											icon: Ext.MessageBox.INFO
													});
												Ext.getCmp("foldout_panel").layout.setActiveItem(0);
											} else {
												//Ext.Msg.alert("失败",d.data);
												Ext.Msg.show({
		   											title:"提示",
		   											msg:"操作失败!",
		   											buttons:Ext.Msg.OK,
		   											icon: Ext.MessageBox.ERROR
													});
											}
										},
										failure: function(response, opts){
											
										}
							    	});
							    }
							});									
						}else{
							alertMsg("没有选中任何行信息，无法进行删除操作!");
						}
					}
				}]
	};
	var p2 = {
		id:"areaform",
		columnWidth: 1,
		xtype :"form",
		title:"模板基本信息",
		labelWidth : 70,
		bodyStyle : "padding:5px 0 0",
		defaultType : "textfield",
		items : [
			{id:"prod_no",fieldLabel:"产品编号",anchor:"100%",hidden:true},
			{id:"businpnt_no",fieldLabel:"外包商编号",anchor:"100%",hidden:true},
			{id:"fold_id",fieldLabel:"插页编号",anchor:"100%",hidden:true},
			{id:"prod_name",fieldLabel:"卡产品名称",anchor:"100%",disabled:true},
			{id:"businpnt_name",fieldLabel:"外包商名称",anchor:"100%",xtype:"billCombox",
				store:bus_store,
				listeners:{
					"select":function(){
						p2_store.load({params:{"fp.businpnt_no":this.getValue()}});
						Ext.getCmp("businpnt_no").setValue(this.getValue());	
					}	
				}
			},
			{id:"period_begen",fieldLabel : "开始月",vtype: 'daterange',
       		endDateField: 'period_end',anchor:"100%",xtype:"datefield",format:"Y-m",editable :false},
			{id:"period_end",fieldLabel : "截止月",anchor:"100%",vtype: 'daterange',
       		startDateField: 'period_begen',xtype:"datefield",format:"Y-m",editable :false},
       		{
       			//xtype:"billCombox", store:sys_store.stmtdate
       			id:"period",fieldLabel:"账单日",anchor:"100%",xtype:"billCombox",store:sys_store.stmtdate
       		},sys_store.checkStmtdate,{
				xtype:"editorgrid",
				enableColumnMove:false,
				enableHdMenu:false,
				title:"插页纸槽个性化设置",
				loadMask : true,
				autoExpandColumn:"feprod_name",//自动填充
				autoExpandMin : 100,//自动填充最小宽度
				autoHeight:true,
				border:false,
				store :p2_store,
				headersDisabled: true,
				columns : [ new Ext.grid.RowNumberer( {header : "序号",width : 35}), 
		            {dataIndex : "foldout_index",header : "插页纸槽编号", menuDisabled: true,id:"fe_index"},
		            {dataIndex : "foldout_prod_name",header : "插页纸名称", menuDisabled: true,id:"feprod_name",
		            editor:new Ext.form.TextField({
			            	allowBlank: false,
			            	maxLength : 10,//最大输入长度
			            	msgTarget :"under"
			            })
					},
					 {dataIndex : "price",header : "插页纸广告费用",id:"price",menuDisabled: true,
		            editor:new Ext.form.NumberField({
			            	allowBlank: false,
			            	allowNegative:false,			            	
			            	maxValue:10000,
			            	maxValueText :"最大为9999.99,小数点后最多保留3位!",
			            	decimalPrecision : 3,
			            	minValue:0,
			            	maxValueText :"最小值为0!",
			            	msgTarget :"under"
			            })
					},
		            {dataIndex : "foldout_prod_weight",header : "克重", menuDisabled: true,
		            editor:new Ext.form.NumberField({
		            		id:"feprod_weight",
		            		allowBlank: false,
		            		allowNegative:false,		
			            	maxValue :100,//最大输入数		            	
			            	maxLengthText :"最大为100!",
			            	minValue:1,
			            	maxValueText :"最小值为1!",
			            	msgTarget :"under"
			            })
		            },
				    {dataIndex : "foldout_pri",id:"fe_pri",header : "优先级", menuDisabled: true,
				    	editor:new Ext.form.NumberField({
		            		allowBlank: false,//输入值大约0
		            		allowNegative:false,//不能输入负号
			            	maxValue :99,//最大输入数		            	
			            	maxLengthText :"最大为99!",
			            	minValue:1,
			            	maxValueText :"最小值为1!",
			            	msgTarget :"under"
			            })
				    }
				 ]
		  	}
		],
		buttonAlign : "center",
		buttons : [{
			id: 'save',
			text : "保存",
			handler : function(e) {
				if(!Ext.getCmp("areaform").getForm().isValid()){
					alertMsg("验证不通过!");
					return;
				}
				e.setDisabled(true);
				var prod_no = Ext.getCmp("prod_no").getValue();
				var businpnt_no = Ext.getCmp("businpnt_no").getValue();
				var fold_id = Ext.getCmp("fold_id").getValue();
				var period;
				if(opr_type == "foldOut!addfocedate.action"){
					period="";
					for(var i=0;i<sys_store.checkedStmtdate.length;i++){
						period+=sys_store.checkedStmtdate[i].boxLabel+",";
					}
					period=period.substr(0,period.length-1);
				}else{
					period= Ext.getCmp("period").getRawValue();
				}
				var period_begen = Ext.getCmp("period_begen").getValue();
				var period_end = Ext.getCmp("period_end").getValue();
				var params = {
						"fp.id":fold_id,
						"fp.prod_no":prod_no,
						"fp.businpnt_no":businpnt_no,
						"fp.period":period,
						"fp.period_begen":period_begen,
						"fp.period_end":period_end
					};
					
				for(var i=0;i<p2_store.getCount();i++){
					
					var data = p2_store.getAt(i).data;
					params["fpbs["+i+"].foldout_index"] = data.foldout_index;
					params["fpbs["+i+"].foldout_prod_name"] = data.foldout_prod_name;
					params["fpbs["+i+"].foldout_prod_weight"] = data.foldout_prod_weight;
					params["fpbs["+i+"].price"] = data.price;
					params["fpbs["+i+"].foldout_pri"] = data.foldout_pri;
				}
				Ext.Ajax.request({
					clientValidation: true,
					params: params,
					url: opr_type,
					success: function(response, opts){
						var d=eval("("+response.responseText+")");
						if(d.code == "1"){
							Ext.getCmp("review").doRefresh();							
							Ext.getCmp("foldout_panel").layout.setActiveItem(0);
							//Ext.Msg.alert("成功",d.data);
							Ext.Msg.show({
		   						title:"提示",
		   						msg:"操作成功!",
		   						buttons:Ext.Msg.OK,
		   						icon: Ext.MessageBox.INFO
								});
									
							Ext.getCmp("save").setDisabled(false);
						} else {
							//Ext.Msg.alert("失败",d.data);
							Ext.Msg.show({
		   						title:"提示",
		   						msg:"操作失败!",
		   						buttons:Ext.Msg.OK,
		   						icon: Ext.MessageBox.ERROR
								});
						}
						
	                    Ext.getCmp("save").setDisabled(false);
					}
				});
		  }
		},{
			text : "取消",
			handler : function(){
				Ext.getCmp("foldout_panel").layout.setActiveItem(0);
			}
		}],
		setParam : function(data) {
			Ext.getCmp("stmtdate").hide();
			Ext.getCmp("prod_no").setValue(data.prod_no);
			Ext.getCmp("businpnt_no").setValue(data.businpnt_no);
			Ext.getCmp("fold_id").setValue(data.id);	
			Ext.getCmp("prod_name").setValue(data.prod_name);
			Ext.getCmp("businpnt_name").setValue(data.businpnt_name)
									 .setDisabled(true);
			Ext.getCmp("period").setRawValue(data.period);
			Ext.getCmp("period").show();
			Ext.getCmp("period_begen").setValue(data.period_begen);
			Ext.getCmp("period_end").setValue(data.period_end);
			p2_store.load({params:{"fp.id":data.id,"fp.businpnt_no":data.businpnt_no}});
		},
		resetParam : function(id,name) {
			/*feId_store.load({
				callback:function(){
					Ext.getCmp("fold_id").setValue(this.getAt(0).data.id);
				}
			});	*/
			Ext.getCmp("stmtdate").show().reset();
			Ext.getCmp("prod_no").setValue(id);
			Ext.getCmp("prod_name").setValue(name);
			Ext.getCmp("businpnt_name").setValue("")
									  .setDisabled(false);
			Ext.getCmp("period").hide();
			Ext.getCmp("period_begen").setValue("");
			Ext.getCmp("period_end").setValue("");
		}
	};
	
	var foldout_pel = new Ext.Panel( {
		id:"master_panel",
		border : false, 
		layout : "border",
		items:[{
			id:"foldout_tree",
			boxMinHeight : 300,//最小高度
			xtype : "billTreePanel",
			dataUrl : "tree!product.action",
			root : new Ext.tree.AsyncTreeNode( {
				id : "root",
				expanded : true // 自动展开节点
					}),
			listeners : {
				"click"	: function(node) {
				    if(node.isLeaf()){
						Ext.getCmp("times").setValue("");//清空查询项
						Ext.getCmp("types").setValue("");//清空查询项
				    	p1_store.baseParams = {};
			    		p1_store.baseParams["fp.prod_no"] = node.id;
			    		p1_store.load({params:{start : 0,limit : limit}});
						Ext.getCmp("foldout_panel").layout.setActiveItem(0);
				    }
				}
			}
		}, {
			id:"foldout_panel",
			region: "center",
			border : false,		
			layout: "card",
			activeItem: 0,
			items: [p1,p2]
		} ]	
	});	
	var CenterPanel=Ext.getCmp("CenterPanel");
	CenterPanel.removeAll();
	CenterPanel.add(foldout_pel);
	CenterPanel.doLayout();
</script>